﻿using CefSharp.WinForms;
using Microsoft.Win32;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Diagnostics;
using System.Drawing;
using System.Linq;
using System.Runtime.InteropServices;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using XLMS.Utils;

namespace ExamClient
{
    public partial class FormExaming : Form
    {
        ChromiumWebBrowser webBrowser;
        public string ExamUrl = "";
        public FormExaming(string url)
        {
            InitializeComponent();

            this.ExamUrl = TranslateUtils.DecryptStringBySecretKey(url, "kaoshiduanclient");
            this.TopMost = true;
            this.WindowState = FormWindowState.Maximized;

            
            try
            {
                Destroy();
            }
            catch
            { }

            if (Screen.AllScreens.Length > 1)
            {
                this.DesktopLocation = Screen.AllScreens[0].Bounds.Location;
            }

            #region  启动浏览器访问
            webBrowser = new ChromiumWebBrowser($"{this.ExamUrl}");
            webBrowser.Dock = DockStyle.Fill;//铺满            
            this.Controls.Add(webBrowser);//加⼊窗体
            #endregion

        }

        private void Destroy()
        {
            Hook_Start();
            IntPtr handleTaskMask = FindWindowEx(IntPtr.Zero, IntPtr.Zero, "Shell_TrayWnd", null);
            ShowWindow(handleTaskMask, 0);
            IntPtr handleDeskTop = FindWindowEx(IntPtr.Zero, IntPtr.Zero, "Progman", null);
            ShowWindow(handleDeskTop, 0);
            SetCtrlAltDeleteStatus(1);
            bool p = true;
            SystemParametersInfo(spi_screensaverrunning, 1, ref p, 0);
        }

        public void SetCtrlAltDeleteStatus(int value)
        {
            //1不可用 0可用

            Microsoft.Win32.RegistryKey key = Microsoft.Win32.Registry.CurrentUser;
            Microsoft.Win32.RegistryKey key1 = key.CreateSubKey("Software\\Microsoft\\Windows\\CurrentVersion\\Policies\\System");
            key1.SetValue("DisableTaskMgr", 1, Microsoft.Win32.RegistryValueKind.DWord);

            RegistryKey r;
            r = Registry.CurrentUser.OpenSubKey("Software\\Microsoft\\Windows\\CurrentVersion\\Policies\\System", true);
            if (r == null)
            {
                r = Registry.CurrentUser.CreateSubKey("Software\\Microsoft\\Windows\\CurrentVersion\\Policies\\System");
            }
            r.SetValue("DisableTaskMgr", value, RegistryValueKind.DWord);
        }

        //委托 
        public delegate int HookProc(int nCode, int wParam, IntPtr lParam);
        static int hHook = 0;
        public const int WH_KEYBOARD_LL = 13;
        //LowLevel键盘截获，如果是WH_KEYBOARD＝2，并不能对系统键盘截取，Acrobat Reader会在你截取之前获得键盘。 
        HookProc KeyBoardHookProcedure;
        //键盘Hook结构函数 
        [StructLayout(LayoutKind.Sequential)]

        public class KeyBoardHookStruct
        {
            public int vkCode;
            public int scanCode;
            public int flags;
            public int time;
            public int dwExtraInfo;
        }
        #region DllImport
        //设置钩子 
        [DllImport("user32.dll")]
        public static extern int SetWindowsHookEx(int idHook, HookProc lpfn, IntPtr hInstance, int threadId);
        [DllImport("user32.dll", CharSet = CharSet.Auto, CallingConvention = CallingConvention.StdCall)]
        //抽掉钩子 
        public static extern bool UnhookWindowsHookEx(int idHook);
        [DllImport("user32.dll")]
        //调用下一个钩子 
        public static extern int CallNextHookEx(int idHook, int nCode, int wParam, IntPtr lParam);

        [DllImport("kernel32.dll")]
        public static extern int GetCurrentThreadId();

        [DllImport("kernel32.dll")]
        public static extern IntPtr GetModuleHandle(string name);

        [DllImport("User32.dll")]
        public static extern IntPtr FindWindowEx(IntPtr ph, IntPtr ch, String cn, String wn);
        [DllImport("User32.dll", CallingConvention = CallingConvention.ThisCall)]
        public static extern bool ShowWindow(IntPtr hWnd, long nCmdShow);

        private const uint spi_screensaverrunning = 97;
        [DllImport("user32.dll", CharSet = CharSet.Auto)]
        private static extern bool SystemParametersInfo(uint uiAction, uint uiParam, ref bool pvParam, uint fWinIni);

        #endregion

        #region 自定义事件
        public void Hook_Start()
        {

            // 安装键盘钩子 
            if (hHook == 0)
            {
                KeyBoardHookProcedure = new HookProc(KeyBoardHookProc);
                hHook = SetWindowsHookEx(WH_KEYBOARD_LL,
                  KeyBoardHookProcedure,
                GetModuleHandle(Process.GetCurrentProcess().MainModule.ModuleName), 0);

                //如果设置钩子失败. 
                if (hHook == 0)
                {
                    Hook_Clear();
                }
            }
        }

        //取消钩子事件 
        public void Hook_Clear()
        {
            bool retKeyboard = true;
            if (hHook != 0)
            {
                retKeyboard = UnhookWindowsHookEx(hHook);
                hHook = 0;
            }
            //如果去掉钩子失败. 
            if (!retKeyboard) throw new Exception("UnhookWindowsHookEx failed.");
        }

        //这里可以添加自己想要的信息处理 
        public static int KeyBoardHookProc(int nCode, int wParam, IntPtr lParam)
        {
            if (nCode >= 0)
            {
                KeyBoardHookStruct kbh = (KeyBoardHookStruct)Marshal.PtrToStructure(lParam, typeof(KeyBoardHookStruct));


                if (kbh.vkCode == (int)Keys.S && (int)Control.ModifierKeys == (int)Keys.Control)  // 截获Ctrl+S
                {
                    //return 1;
                }
                if (kbh.vkCode == (int)Keys.C && (int)Control.ModifierKeys == (int)Keys.Control)  // 截获Ctrl+C
                {
                    //return 1;
                }
                if (kbh.vkCode == (int)Keys.V && (int)Control.ModifierKeys == (int)Keys.Control)  // 截获Ctrl+V
                {
                    return 1;
                }

                if (kbh.vkCode == (int)Keys.Y && (int)Control.ModifierKeys == (int)Keys.Control + (int)Keys.Alt)  //截获Ctrl+Alt+Y 
                {
                    return 1;
                }

                if (kbh.vkCode == (int)Keys.LWin) //截获win
                {
                    return 1;
                }
                if (kbh.vkCode == (int)Keys.Tab && (int)Control.ModifierKeys == (int)Keys.Alt) //截获Ctrl+Tab
                {
                    return 1;
                }
                if (kbh.vkCode == (int)Keys.F5) //截获F5
                {
                    return 1;
                }
                if (kbh.vkCode == (int)Keys.F4 && (int)Control.ModifierKeys == (int)Keys.Alt)//截获Alt + F4
                {
                    //不能测底封死退出的路径，否则一旦交不了卷，退都退不出来，快赶上病毒了
                    ////return 1;
                }

                if (kbh.vkCode == (int)Keys.E && (int)Control.ModifierKeys == (int)Keys.LWin)//截获win + e
                {
                    return 1;
                }

                if ((int)Control.ModifierKeys == (int)Keys.LWin)
                {
                    return 1;
                }
                if (kbh.vkCode == (int)Keys.Escape && (int)Control.ModifierKeys == (int)Keys.Alt)////截获Alt + esc
                {
                    return 1;
                }

                if ((int)Control.ModifierKeys == (int)Keys.Control + (int)Keys.Alt + (int)Keys.Delete)//截获Ctrl+Alt+Delete 不成功 
                {
                    return 1;
                }
                if ((int)Control.ModifierKeys == (int)Keys.Control + (int)Keys.Alt + (int)Keys.Tab)//截获Ctrl+Alt+Tab 不成功 
                {
                    return 1;
                }
                if (kbh.vkCode == (int)Keys.Escape && (int)Control.ModifierKeys == (int)Keys.Control)////截获Ctrl + esc
                {
                    return 1;
                }/**/
            }
            return CallNextHookEx(hHook, nCode, wParam, lParam);
        }
        #endregion
    }
}
